using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace ComponentSystem
{
    /// <summary>
    /// Mono 实体
    /// </summary>
    [DisallowMultipleComponent]
    public partial class EntityMono : MonoBehaviour, IState
    {
        /// <summary>
        /// 核心实体实例
        /// </summary>
        private EntityCSharp _entityCSharp;

        /// <summary>
        /// 访问原始Entity的快捷方式
        /// </summary>
        public EntityCSharp entityCSharp => _entityCSharp ??= AddCharpEntity<EntityCSharp>();

        #region 组件操作

        /// <summary>
        /// 添加组件
        /// </summary>
        public K AddComponentCSharp<K>() where K : Component, new() => entityCSharp?.AddComponent<K>();

        /// <summary>
        /// 移除组件
        /// </summary>
        public void RemoveComponentCSharp<K>() where K : Component => entityCSharp?.RemoveComponent<K>();

        /// <summary>
        /// 获取组件
        /// </summary>
        public K GetComponentCSharp<K>() where K : Component => entityCSharp?.GetComponent<K>();

        #endregion

        #region 实体操作

        public K AddCharpEntity<K>() where K : EntityCSharp
        {
            _entityCSharp = ComponentFactory.CreateEntityCSharpByMono<K>(this);
            return _entityCSharp as K;
        }

        #endregion


        /// <summary>
        /// 组件状态
        /// </summary>
        public UniversalEnum.State state { get; protected set; } = UniversalEnum.State.Init;

    }
    public partial class EntityMono
    {
        /// <summary>
        /// MonoBehaviour生命周期与Entity生命周期绑定
        /// </summary>
        public virtual void Initialization()
        {

            // 自动添加Unity组件适配器 样例
            //if (!TryGetComponent<UnityComponentAdapter>(out _))
            //{
            //    gameObject.AddComponent<UnityComponentAdapter>();
            //}

            this.UpdateState(UniversalEnum.State.IsInited);
        }

        /// <summary>
        /// 销毁
        /// </summary>
        protected virtual void OnDestroy()
        {
            Do_Destroy();
        }

        /// <summary>
        /// 执行自定义销毁
        /// </summary>
        public virtual void Do_Destroy()
        {
            if (IsDisposed()) return;
            this.Dispose();
        }

        /// <summary>
        /// 销毁C#实体
        /// </summary>
        public virtual void DisposeSharpEntity()
        {
            if (_entityCSharp == null || _entityCSharp.IsDisposed())
            {
                return;
            }
            _entityCSharp?.Dispose();
            _entityCSharp = null;
        }

        /// <summary>
        /// 更新状态
        /// </summary>
        /// <param name="state">状态</param>
        /// <returns>状态</returns>
        public UniversalEnum.State UpdateState(UniversalEnum.State state)
        {
            this.state = state;
            return this.state;
        }

        #region 状态管理

        public bool IsStart() => this.state == UniversalEnum.State.Start;
        public bool IsStop() => this.state == UniversalEnum.State.Stop;
        public bool IsPause() => this.state == UniversalEnum.State.Pause;
        public bool IsResume() => this.state == UniversalEnum.State.Resume;
        public bool IsReset() => this.state == UniversalEnum.State.Reset;
        public bool IsDisposed() => this.state == UniversalEnum.State.Disposed;

        /// <summary>
        /// 重新开始
        /// </summary>
        public virtual void ReStart()
        {
            if (this.IsStart())
            {
                return;
            }
            this.UpdateState(UniversalEnum.State.Start);
            _entityCSharp?.ReStart();
        }

        /// <summary>
        /// 停止
        /// </summary>
        public virtual void Stop()
        {
            if (this.IsStop())
            {
                return;
            }
            this.UpdateState(UniversalEnum.State.Stop);
            _entityCSharp?.Stop();
        }

        /// <summary>
        /// 暂停
        /// </summary>
        public virtual void Pause()
        {
            if (this.IsPause())
            {
                return;
            }
            this.UpdateState(UniversalEnum.State.Pause);
            _entityCSharp?.Pause();
        }

        /// <summary>
        /// 恢复
        /// </summary>
        public virtual void Resume()
        {
            if (this.IsResume())
            {
                return;
            }
            this.UpdateState(UniversalEnum.State.Resume);
            _entityCSharp?.Resume();
        }

        /// <summary>
        /// 重置
        /// </summary>
        public virtual void Reset()
        {
            if (this.IsReset())
            {
                return;
            }
            this.UpdateState(UniversalEnum.State.Reset);
            _entityCSharp?.Reset();
        }

        /// <summary>
        /// 销毁
        /// </summary>
        public virtual void Dispose()
        {
            if (this.IsDisposed())
            {
                return;
            }
            this.UpdateState(UniversalEnum.State.Disposed);
            DisposeSharpEntity();
        }

        #endregion
    }

    /// <summary>
    /// TODO 这是保留一个样例 暂时没有使用
    /// 关键适配器组件（将MonoBehaviour事件转发给Entity组件）
    /// </summary>
    public class UnityComponentAdapter : MonoBehaviour
    {
        /// <summary>
        /// 实体Mono
        /// </summary>
        private EntityMono _entityMono;
        private EntityCSharp entityCSharp => _entityMono.entityCSharp;

        /// <summary>
        /// Awake
        /// </summary>
        private void Awake()
        {
            _entityMono = GetComponent<EntityMono>();
            if (_entityMono == null) Destroy(this);
        }

        /// <summary>
        /// Update
        /// </summary>
        private void Update()
        {
            if (_entityMono == null || entityCSharp == null) return;
            foreach (var comp in entityCSharp.GetComponents())
            {
                if (comp is IUpdatable updatable)
                {
                    updatable.OnUpdate(Time.deltaTime);
                }
            }
        }
    }
}